<?php

/**
 * 默认控制器
 */
class Controller_Astgo extends Controller_Abstract
{
	private $leftchar = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><link href="css/css.css" rel="stylesheet" type="text/css"/><head ><title>DoCall</title></head><body><body style="text-align:center">
	
	
	';
	private $rightchar = '</body></html>';
	
	function _response($data)
	{
		exit($this->leftchar.$data.$this->rightchar);
	}
	function deString($string)
	{
		$param = explode('&', $string);
		$result = array();
		foreach($param as $k=>$v)
		{
			$split = explode('=', $v);
			$result[$split[0]] = $split[1];
		}
		return $result;
	}
	function actionwap()
	{
		$arr = $this->deString($_SERVER['QUERY_STRING']);
		if (isset($arr['b']))
		{
			$mobile = isset($arr['a'])?$arr['a']:'';
			$phone = isset($arr['b'])?$arr['b']:'';
			if(!Helper_Param::_phone($mobile))
			{
				$result = 'ERR1'; //手机号码格式不正确
			}else{
				$user = Acctuser::checkAcctname($mobile);
				if (!$user->id()){
					$result = 'ERR2'; //不存在此绑定号码
				}else if($user->callpwd == 1)
				{
					$result = 'ERR2';//设置为加密呼叫了	
				}else{
					$isone = Cbsubmit::find('bindname = ? and destination_number = ?',$mobile,$phone)->getCount('id');
					if($isone){
						$result = 'ERR3';//存在此呼叫队列 请勿重复提交
					}else{
						if(!Helper_Param::_phone($phone))
						{
							$result = 'ERR5';//被叫号码格式不正确
						}else{
							Cbsubmit::call($mobile, $phone);
							$result = '1';//提交成功
						}
			
					}
				}
			}
			$this->_response($result);
			#预约
		}else{
			#余额
			$mobile = isset($arr['a'])?$arr['a']:'';
			if (!Helper_Param::_phone($mobile))
			{
				$result = sprintf('%f',0);
			}else
			{
				$user = Acctuser::checkAcctname($mobile);
				//$bind = Userbindtel::find('bindtel = ?',$mobile)->setColumns('id,acctname')->getOne();
				if(!$user->id())
				{
					$result = sprintf('%f',0);
				}else{
					$result = sprintf('%f',isset($user->balance)?$user->balance:0);
				}
			}
			$this->_response($result);
		}
	}
	function actionpay()
	{
		$arr = $this->deString($_SERVER['QUERY_STRING']);
		$mobile = isset($arr['caller'])?$arr['caller']:'';
		$cardid = isset($arr['cardid'])?$arr['cardid']:'';
		$cardpwd = isset($arr['cardpwd'])?$arr['cardpwd']:'';
		if(!Helper_Param::_call($mobile))
		{
			$result = 'ERR1'; //手机号码格式不正确
		}else{
			//$user = Acctuser::checkAcctname($mobile);
			//if(!$user->id())
			//{
			//	$result = 'ERR2'; //不存在此绑定号码
			//}else{
			$flag = Acctuser::use_card($mobile, $cardid, $cardpwd);
			if($flag === true)
			{
				$card = Vouchercard::find('cardname = ? and password = ?',$cardid,$cardpwd)->setColumns('id,balance')->getOne();
				$result = sprintf('%f',$card->balance);
			}else{
				$result = 'ERR3';
			}
			//}
		}
		$this->_response($result);
	}
}